package com.atguigu;

import java.util.HashMap;

/**
 * projectName: com.atguigu
 *
 * @author: 顾司羽
 * time: 2025/2/10 8:48 周一
 * description: 力扣第一题
 */
// TODO: 2025/2/10
//  输入：nums = [2,7,11,15], target = 9
//  输出：[0,1]
//  解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。

public class LeetCode01 {
    public static int[] twoSum(int[] nums, int target) {
/*            for (int i = 0; i < nums.length; i++) {
                for (int j = i + 1; j < nums.length + 1; j++) {
                    if (nums[i] + nums[j] == target) {
                        return new int[]{i, j};
                    }
                }
            }
            return null;
        }*/

        HashMap<Integer, Integer> map = new HashMap<>(nums.length);
        for (int i = 0; i < nums.length; i++) {
            int partnerNumber = target - nums[i];
            if (map.containsKey(partnerNumber)) {
                return new int[]{map.get(partnerNumber), i};
            }
            map.put(nums[i], i);
        }
        return null;
    }

    public static void main(String[] args) {
        int[] nums = {2, 7, 11, 15};
        int target = 9;
        try {
            int[] result = twoSum(nums, target);
            System.out.println("Indices: " + result[0] + ", " + result[1]);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }
    }
}
